<?php
	/*
	 * Created by Nicky
	 *
	*/
	 /* permission name 
	 akademik_gdp_pengajarpelajaran
	 akademik_gdp_pengajarpelajaran_add
	 akademik_gdp_pengajarpelajaran_edit
	 akademik_gdp_pengajarpelajaran_del	 
	 */	
	class Pengajar_pelajaranController extends CController{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Master Akademik";
		public $subMenu = "";
		public $title = "";		
		public function init() {
			if (!LoginUser::isLogin("akademik")){
				//$this->redirect(array('login/akademik','error'=>Yii::t("label", "Username atau password salah")));
				LoginUser::redirectToLoginPage("akademik");				
			}
						
		}
		
		public function actionIndex(){
			LoginUser::getPermission("akademik_gdp_pengajarpelajaran");
			$this->subMenu = "PengajarPelajaran";
			$this->layout ="//layouts/ly_blank";
			$data=array("department_id"=>"");			
			$this->render('index',$data);
		}
		
		public function actiondetailList(){		
			$onlyActive = Yii::app()->request->getParam("activeOnly",false);
			$department_id = Yii::app()->request->getParam("department_id","KB");
			$karyawan_id = Yii::app()->request->getParam("karyawan_id","0");		
			$mcon= Yii::app()->db;
			if($karyawan_id==0){$department_id="00";} else {$department_id=$department_id;}
			$sql1='select m_pelajaran.id as id,department_id,name,singkatan,keterangan,is_active,case when mpt.id is null then "0" else "1" end as checkstat,karyawan_id from m_pelajaran LEFT OUTER JOIN (select * from m_pengajar_pelajaran where karyawan_id=:karyawan_id) mpt on m_pelajaran.id=pelajaran_id where department_id=:department_id and is_active=True ';
			$mPengajarPelajarancom=$mcon->createCommand($sql1);
			$mPengajarPelajarancom->bindValue('department_id',$department_id);
			$mPengajarPelajarancom->bindValue('karyawan_id',$karyawan_id);
			$mPengajarPelajaran=$mPengajarPelajarancom->queryAll();	
			
/*			if ($onlyActive){
				$mPengajarPelajaran = MPengajarPelajaran::model()->findAll(array("condition"=>"is_active = TRUE and department_id = $department_id","order"=>"id"));
			}else{
				$mPengajarPelajaran = MPengajarPelajaran::model()->findAll(array("condition"=>"department_id = $department_id","order"=>"id"));
			}			
*/			$this->result["actionStatus"] =0;
			$this->result["actionMessage"] ="";
			$this->result["actionError"] =array();
			$this->result["model"] =  $mPengajarPelajaran;			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
			 
		}
		
		public function actionedit(){		
 
			$op = Yii::app()->request->getParam("op","add");
 			$status = Yii::app()->request->getParam("is_active",0) === "on"?1:0 ;			
			if ($op == "add"){
				LoginUser::getPermission("akademik_gdp_pengajarpelajaran_add");
				//print_r ($_POST);
				//echo "DATA SUDAH DI ADD........";
				$pengajarpelajaran = new MPengajarPelajaran;
				$pengajarpelajaran->department_id = Yii::app()->request->getParam("department_id","");
				$pengajarpelajaran->name = Yii::app()->request->getParam("name","");
				$pengajarpelajaran->singkatan = Yii::app()->request->getParam("singkatan","");
				$pengajarpelajaran->keterangan = Yii::app()->request->getParam("keterangan","");				
				$pengajarpelajaran->is_active=$status;
				if ($pengajarpelajaran->save()){
					echo "<script>
						alert('".Yii::t("dialog","Data sudah di tambahkan...!"). "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($pengajarpelajaran->getErrors());
				}				
			}else if ($op == "edit"){
				LoginUser::getPermission("akademik_gdp_pengajarpelajaran_edit");
				//print_r ($_POST);
				$id = Yii::app()->request->getParam("id","");				
				$pengajarpelajaran = MPengajarPelajaran::model()->find("id=:id",array(":id"=>$id));
				$pengajarpelajaran->name = Yii::app()->request->getParam("name","");
				$pengajarpelajaran->singkatan = Yii::app()->request->getParam("singkatan","");
				$pengajarpelajaran->keterangan = Yii::app()->request->getParam("keterangan","");					
				$pengajarpelajaran->is_active=$status;
				if ($pengajarpelajaran->save()){
					echo "<script>
						alert('".Yii::t("dialog","Perubahan data sudah di simpan...!"). "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($pengajarpelajaran->getErrors());
				}				
			}else if ($op == "del"){
				if (!LoginUser::getPermission("akademik_gdp_pengajarpelajaran_del",null,false)){
					$this->result["actionStatus"] =1;
					$this->result["actionMessage"] = Yii::t("alert","Anda tidak memiliki ijin untuk menghapus data ini !");
					$this->result["actionError"] =array();
					$this->result["model"] = array();
					header('Content-type:application/json');
					echo CJSON::encode($this->result);
					return;
				}
				$id = Yii::app()->request->getParam("id","");
				$conn = Yii::app()->db;
				$sql = "delete from m_pengajar_pelajaran where id = :id";
				$command = $conn->createCommand($sql);
				try{
					//$command->bindParam(":id",$id,PDO::PARAM_INT);
					if ($command->execute(array(":id"=>$id))){
						$this->result["actionStatus"] =0;
						$this->result["actionMessage"] ="";
						$this->result["actionError"] =array();
						$this->result["model"] = array();
					}
				}catch(Exception $e){
					$this->result["actionStatus"] =2;
					$this->result["actionMessage"] = $e->getMessage();
					$this->result["actionError"] =array();
					$this->result["model"] = array();
				}
				Yii::app()->db->active = false;
			}else if ($op == "update"){
				$id = Yii::app()->request->getParam("id","");
				$stat = Yii::app()->request->getParam("stat","");
				$karyawan_id = Yii::app()->request->getParam("karyawan","");				
			 	if ($stat==0){ 		
				$conn = Yii::app()->db;
				$sql = "delete from m_pengajar_pelajaran where pelajaran_id = ".$id." and karyawan_id =".$karyawan_id;
				$command = $conn->createCommand($sql);
 				}
				else
				{
				$conn = Yii::app()->db;
				$sql = "insert into m_pengajar_pelajaran (karyawan_id,pelajaran_id) values (".$karyawan_id.",".$id.")";
				$command = $conn->createCommand($sql);
									
				}
				try{
					//$command->bindParam(":id",$id,PDO::PARAM_INT);
					if ($command->execute(array(":id"=>$id))){
						$this->result["actionStatus"] =0;
						$this->result["actionMessage"] ="";
						$this->result["actionError"] =array();
						$this->result["model"] = array();
					}
				}catch(Exception $e){
					$this->result["actionStatus"] =2;
					$this->result["actionMessage"] = $e->getMessage();
					$this->result["actionError"] =array();
					$this->result["model"] = array();
				}
				Yii::app()->db->active = false;
				
			}
			
			header('Content-type:application/json');
			echo CJSON::encode($this->result);
		}
		
		public function actionform(){
			$op = Yii::app()->request->getParam("op","add");
			$callback = Yii::app()->request->getParam("callback","");
			$department_id = Yii::app()->request->getParam("department_id","0");
			
			$data = array("op"=>"add","callback"=>"","id"=>0,"department_id" =>$department_id,"name"=>"","singkatan"=>"","keterangan"=>"","is_active"=>"1");
			
			if ($op == "add"){
				LoginUser::getPermission("akademik_gdp_pengajarpelajaran_add");
				$data["op"] = "add";
				$data["callback"] = $callback;				
			}else if($op =="edit"){	
				LoginUser::getPermission("akademik_gdp_pengajarpelajaran_edit");						
				$data["op"] = "edit";
				$data["callback"] = $callback;				
				$id = Yii::app()->request->getParam("id","0");				
				$data["id"] = $id;
				
				$rsPengajarPelajaran = MPengajarPelajaran::model()->findAll(array("condition"=>"id = $id","order"=>"id"));
				if (count($rsPengajarPelajaran) > 0){
					$data["department_id"] = $rsPengajarPelajaran[0]->department_id;
					$data["name"] = $rsPengajarPelajaran[0]->name;
					$data["singkatan"] = $rsPengajarPelajaran[0]->singkatan;
					$data["keterangan"] = $rsPengajarPelajaran[0]->keterangan;					
					$data["is_active"] = $rsPengajarPelajaran[0]->is_active;
				}				
			}			
			$this->layout ="//layouts/ly_blank";
			$this->render("form",$data);
		}
				
		protected function beforeRender($view){
			if ($this->subMenu != ""){
				$this->title = $this->mainMenu . " - " .$this->subMenu;
			}
			return true;
		}
		
	}
?>